<?php
$path =  str_replace('/wp-content/plugins/stSocialMediaToolbox/Toolboxes/stFacebookToolbox/cronjob','',dirname(__FILE__));
require($path.'/wp-config.php');
    global $wpdb;
        $data['cronjob_name']  = 'cronjob Facebook Sitemap';
    $data['toolbox_name'] = 'stFacebookToolbox';
    $data['url'] = dirname(__FILE__).'/sitemap.php';
    $data['status'] = 0;
	$data['ctime'] = date_i18n('Y-m-d H:i:S');
    $wpdb->insert('wp_st_cronjob_log',$data);
    $log_id = $wpdb->get_var("SELECT MAX(id) FROM wp_st_cronjob_log");
    $result = $wpdb->get_results("SELECT * FROM st_sitemap");
    foreach($result as $row){
             $wpdb->query("DELETE FROM st_sitemap_link WHERE user_id=".$row->user_id." AND site_id =".$row->id);
    		if(substr($row->site_url,-4) == '.xml'){
                         $xml = file_get_contents($row->site_url);
                         preg_match_all("/<loc>(.*?)<\/loc>/i",$xml,$array);
             $stt = 0;foreach($array[1] as $name){
                    $wpdb->insert('st_sitemap_link',array('link'=>$name,'user_id'=>$row->user_id,'site_id'=>$row->id));
                        $stt++;
                     }
                     $wpdb->update('st_sitemap',array('number'=>$stt),array('id'=>$row->id));
             }
             else
             {
                 $link = file_get_contents($row->site_url);
                 $array = explode("\n",$link);
                 $stt = 0; foreach($array as $name){
                      if($name != ''){
                    $wpdb->insert('st_sitemap_link',array('link'=>$name,'user_id'=>$row->user_id,'site_id'=>$row->id));
                    $stt++;
                      }

                     }
                     $wpdb->update('st_sitemap',array('number'=>$stt),array('id'=>$row->id));
             }
        }

$wpdb->update('wp_st_cronjob_log',array('status'=>1),array('id'=>$log_id));
?>
